﻿using Android.Media;
using Games.Droid;
using System;
using System.IO;
using Xamarin.Forms;

[assembly: Dependency(typeof(Logger))]
namespace Games.Droid
{
    class Logger : ILogger
    {
        public void Log(string message)
        {
            Java.IO.File dataDirectoryPath = Android.OS.Environment.ExternalStorageDirectory;
            // Get my documents path
            string filePath = Path.Combine(dataDirectoryPath.AbsolutePath, "com.qiuxia.games");

            if(!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            // Combine with filename
            string fullName = Path.Combine(filePath, "log.txt");

            if(message != null)
            {
                var text = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff} {message}\r\n\r\n";
                File.AppendAllText(fullName, text, System.Text.Encoding.UTF8);
            }
        }
        public void Log(string message, Exception exception)
        {
            Java.IO.File dataDirectoryPath = Android.OS.Environment.ExternalStorageDirectory;
            // Get my documents path
            string filePath = Path.Combine(dataDirectoryPath.AbsolutePath, "com.qiuxia.games");

            if(!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            // Combine with filename
            string fullName = Path.Combine(filePath, "log.txt");

            if(exception != null)
            {
                var text = $"{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff} {message ?? "出现异常"}：{exception.Message}\r\n{exception.StackTrace}\r\n\r\n";
                File.AppendAllText(fullName, text, System.Text.Encoding.UTF8);
            }
        }
    }
}


